package com.goudan.practice.array;

import java.util.HashMap;
import java.util.Map;

/**
 * 问题：给定一个数组和一个目标数，从数组中找到两个数，是这两个数之和等于目标数。返回其在数组中的编号
 */
public class FindTwoSumNum {

    public static void main(String[] args) {
        int[] a = {2, 4, 6, 8, 10, 13, 14, 16};
        int[] res = twoSum(a, 16);
        for (int i : res) {
            System.out.println(i);
        }

    }

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);
        }
        return null;
    }
}
